/******************************************************************************
Copyright:
2009 Jens Collin (jens.collin@dinomin.com)

This file is part of Envox Decompiler.

Envox Decompiler is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Envox Decompiler is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KitchenOS.  If not, see <http://www.gnu.org/licenses/>.

******************************************************************************/
using System;
using System.Collections;

namespace EnvoxDecompiler
{
	/// <summary>
	/// Summary description for Block_SetVariable.
	/// </summary>
	public class Block_SetVariable : Block
	{

		private long _noEntries;
		private ArrayList _entries = new ArrayList();


		public override TYPE GetBlockType()
		{
			return TYPE.SetVariable;
		}

		private string GetEntriesXML()
		{
			string xml = "\t\t\t\t<Entries>\r\n";
			foreach (KeyVal kv in _entries)
			{
				string name = Utils.XmlString(kv.GetKey());
				string val = Utils.XmlString(kv.GetValue());
				xml += "\t\t\t\t\t<SetVarEntry var=\"" + name + "\" expr=\"" + val + "\"/>\r\n";
			}
			xml += "\t\t\t\t</Entries>\r\n";
			return xml;
		}


		public override string GetXML(int x, int y)
		{
			Trace.Debug("Block_SetVariable::GetXML Started.");	
			// <SetVar name="NAME" rect="X 32 96 56"
            string xml = "\t\t\t<SetVar name=\"" + Utils.XmlString(_name) + "\" rect=\"" + x + " " + y + " 96 56\"";
			//  ...description="DESC"...
			xml += GetDescriptionXML();
			//  ...clearDTMF="_clearDTMFBuffer"...
			xml += GetClearDTMFXML();
			// ...minUserLevel="_userLevel"
			xml += GetMinUserLevelXML();
			// ...cpMessage=""...
			xml += GetControlPanelMessageXML();
			// End the header xml-part.
			xml += ">\r\n";

			// <Inputs>
			xml += GetInputsXML(_blockId);
		
			// <Outputs>
			xml += GetOutputsXML();

			// <Logs>
			xml += GetLogsXML();
			
			xml += GetHandledErrorsXML();
	
			// Set variable entries
			xml += GetEntriesXML();

			// </SetVar>
			xml +="\t\t\t</SetVar>\r\n";
			return xml;
		}


		private void ReadEntries(byte[] data)
		{
			for (long i=1; i <= _noEntries; i++)
			{
				long size = data[_currentPos] + data[_currentPos + 1] * 256;

				_currentPos += 12;

				// variable name
				string name = Utils.DataToString(data, _currentPos);
				_currentPos = _currentPos + Utils.DataStringLength(data, _currentPos);

				// variable expression
				string expression = Utils.DataToString(data, _currentPos);
				_currentPos = _currentPos + Utils.DataStringLength(data, _currentPos);

				KeyVal kv = new KeyVal(name, expression);
				_entries.Add(kv);
				Trace.Debug("SetVariable[" + i + "/" + _noEntries + "]: " + name + " = " + expression);
			}
		}


		public Block_SetVariable(byte[] rawData) : base(rawData)
		{
			// base has moved _currentPos to our start position.

			outputErrorIds.Add(new BlockOutput(OUTPUT.Next, 0));

			Trace.Debug("Block_SetVariable created.");

			_noEntries = rawData[_currentPos + 4] + rawData[_currentPos + 5] * 256;

			_currentPos += 8;

			ReadEntries(rawData);
		}
	}
}